import { _decorator, Label, Sprite, SpriteAtlas, SpriteFrame } from 'cc';
import { LayerType } from '../../Basic/Config/BaseEnum';
import { MultiPlatform } from '../../Basic/Model/Platform/MultiPlatform';
import { VideoEvent } from '../../Data/Types';
import { EventManager } from '../../Basic/Manager/EventManager';
import { EventType } from '../Config/GameEnum';
import { PropModel } from '../Dialog/PropModel';
import { GetItemModel } from './GetItemModel';
import { DataMgr, PlayerData } from '../../Basic/Manager/DataManager';
import { Tool } from '../../Util/Tool';
import { GEEvent, GravityModel } from '../../Basic/Model/SDK/GravityModel';
import { PlayerParams } from '../../Basic/Config/DataConfig';
import { PanelBase } from '../../Basic/UI/PanelBase';
import { GameModel } from '../Game/GameModel';
const { ccclass, property } = _decorator;

@ccclass('GetItemPanel')
export class GetItemPanel extends PanelBase {
  panelLayer: LayerType.DialogLayer = LayerType.DialogLayer;
  @property(Sprite)
  item: Sprite;
  @property(Label)
  itemName: Label;
  @property(Label)
  itemDesc: Label;
  protected onEnable(): void {
    super.onEnable();

    EventManager.Ins().register(EventType.AD_GETITEM, this.onAd, this);
    this.renderUI();
  }

  protected onDisable(): void {
    EventManager.Ins().unRegister(EventType.AD_GETITEM, this.onAd);
  }

  renderUI() {
    const goodsData = DataMgr.tables.TbColor.get(GetItemModel.Ins().curPropId);
    if (goodsData != null) {
      const imgData = DataMgr.tables.TbImg.get(goodsData.cakeImg);
      // Tool.getSpriteByKey(imgData.altasPath, imgData.imgName).then((spr) => {
      //   this.item.spriteFrame = spr;
      // });
      if (imgData.altasPath === '') {
        GameModel.Ins()
          .loadDynamicRes<SpriteFrame>(imgData.imgName + '/spriteFrame')
          .then((spr) => {
            this.item.spriteFrame = spr;
          });
      } else {
        GameModel.Ins()
          .loadDynamicRes<SpriteAtlas>(imgData.altasPath)
          .then((altas) => {
            this.item.spriteFrame = altas.getSpriteFrame(imgData.imgName);
          });
      }
      this.itemName.string = `${goodsData.name}`;
      this.itemDesc.string = `${goodsData.desc}`;
    } else {
      this.item.spriteFrame = null;
      this.itemName.string = ``;
      this.itemDesc.string = ``;
    }
  }

  onAd() {
    GameModel.Ins().adCount++;
    DataMgr.changePlayerAttr(PlayerParams.adCount, 1);
    // GravityModel.I.userSet({ accuAds: PlayerData.get(PlayerParams.adCount) });
    // GravityModel.I.track(GEEvent.AD_SHOW_END, { adPosition: `看视频领道具${GetItemModel.Ins().curPropId}` });
    PropModel.Ins().addProp(GetItemModel.Ins().curPropId, 1);
    this.clickClose();
  }

  onClickBtn(t: Touch, data) {
    const key = data.toString();
    switch (key) {
      case 'ad':
        // GravityModel.I.track(GEEvent.AD_SHOW, { adPosition: `看视频领道具${GetItemModel.Ins().curPropId}` });
        MultiPlatform.Ins().platformUnit.showVideo(VideoEvent.AddItem);
        break;
      case 'giveup':
        this.clickClose();
        break;
    }
  }
}
